package com.changgou.goods.controller;

import com.changgou.framework.entity.PageResult;
import com.changgou.framework.entity.Result;
import com.changgou.goods.service.CategoryService;
import com.haha.goods.entity.Category;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author Yang
 * @date 2020/11/7
 */
@CrossOrigin
@RestController
@RequestMapping("/category")
public class CategoryController {
    @Autowired
    private CategoryService categoryService;
    /***
     * Category分页条件搜索实现
     * @param category
     * @param page
     * @param size
     * @return
     */
    @PostMapping(value = "/search/{page}/{size}" )
    public Result findPage(@RequestBody(required = false) Category category, @PathVariable  int page, @PathVariable  int size){
        //执行搜索
        PageResult pageInfo = categoryService.findPage(category, page, size);
        return Result.ok(pageInfo);
    }

    /***
     * Category分页搜索实现
     * @param page:当前页
     * @param size:每页显示多少条
     * @return
     */
    @GetMapping(value = "/search/{page}/{size}" )
    public Result findPage(@PathVariable  int page, @PathVariable  int size){
        //分页查询
        PageResult pageInfo = categoryService.findPage(null,page, size);
        return Result.ok(pageInfo);
    }

    /***
     * 多条件搜索品牌数据
     * @param category
     * @return
     */
    @PostMapping(value = "/search" )
    public Result findList(@RequestBody(required = false)  Category category){
        List<Category> list = categoryService.findPage(category,1,Integer.MAX_VALUE).getRows();
        return Result.ok(list);
    }

    /***
     * 根据ID删除品牌数据
     * @param id
     * @return
     */
    @DeleteMapping(value = "/{id}" )
    public Result delete(@PathVariable Integer id){
        boolean b = categoryService.removeById(id);
        return b?Result.ok(null):Result.error("删除失败");
    }

    /***
     * 修改Category数据
     * @param category
     * @param id
     * @return
     */
    @PutMapping(value="/{id}")
    public Result update(@RequestBody  Category category,@PathVariable Integer id){
        //设置主键值
        category.setId(id);
        //修改数据
        boolean b = categoryService.updateById(category);
        return b?Result.ok(null):Result.error("修改失败");
    }

    /***
     * 新增Category数据
     * @param category
     * @return
     */
    @PostMapping
    public Result add(@RequestBody   Category category){
        boolean b = categoryService.save(category);
        return b?Result.ok(null):Result.error("添加失败");
    }

    /***
     * 根据ID查询Category数据
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Result findById(@PathVariable Integer id){
        //根据ID查询
        Category category = categoryService.getById(id);
        return Result.ok(category);
    }

    /***
     * 查询Category全部数据
     * @return
     */
    @GetMapping
    public Result findAll(){
        List<Category> list = categoryService.list();
        return Result.ok(list);
    }

    /**
     * 根据父ID查询
     */
    @RequestMapping(value ="/list/{pid}")
    public Result findByPrantId(@PathVariable(value = "pid")Integer pid){
        if (pid==null){
            return Result.error("请传入父id");
        }
        //根据父节点ID查询
        List<Category> list = categoryService.findByParentId(pid);
        return Result.ok(list);
    }
}
